package com.kstech.mp3604.fragments

import androidx.navigation.fragment.findNavController
import com.blankii.androidlibrary.base.BaseBindingFragment
import com.kstech.mp3604.databinding.FragmentSerialCommBinding
import com.kstech.mp3604.manager.serialComm.SerialCommStatusListener
import com.kstech.mp3604.manager.serialComm.SerialManager
import kotlin.math.abs

/**
 * can 通讯测试
 */
class TestSerialCommFragment :
    BaseBindingFragment<FragmentSerialCommBinding>(FragmentSerialCommBinding::inflate),
    SerialManager.Rev_0210_Listener, SerialManager.Rev_0310_Listener,
    SerialManager.Rev_0410_Listener, SerialCommStatusListener {
    private val stringBuilder = StringBuilder()
    override fun onCreateInit() {

    }

    override fun onInitView() {
        SerialManager.setRev0210Listener(this)
        SerialManager.setRev0310Listener(this)
        SerialManager.setRev0410Listener(this)
        binding.apply {
            //连接串口
//            button1.setOnClickListener {
//                SerialManager.openSerial(requireContext(), this@TestSerialCommFragment)
//            }
            //发送消息
//            button2.setOnClickListener {
//                val inputMsg = inputMsg.text.toString().trim()
//                SerialManager.sendSerial(inputMsg)
//            }
////            //发送心跳
//            button3.setOnClickListener {
//                SerialManager.startHeartBeat()
//            }
            backBtn.setOnClickListener {
                findNavController().navigateUp()
            }
        }
    }


    override fun data_0210(data1: Int, data2: Int, data3: Int, data4: Int) {
        binding.v1.text = "行进速度:${abs(data1)}"
        binding.v2.text = "横移速度:${abs(data2)}"
        binding.v3.text = "起升速度:${abs(data3)}"
//        binding.v4.text = "接近速度:$data4"
    }

    override fun data_0310(data1: Long, data2: Int, data3: Int) {
        binding.v21.text = "X:$data1"
        binding.v22.text = "Y:$data2"
        binding.v23.text = "Z:$data3"
    }

    override fun data1_0410(data1: Int, data2: String) {
        binding.v51.text = "故障索引:$data1"
        binding.v52.text = "故障码:$data2"
    }

    override fun data2_0410(data1: Int, data2: Int, data3: Int) {
        binding.v31.text = "应急:$data1"
        binding.v32.text = "自动回正:$data2"
        binding.v33.text = "自动回位:$data3"
    }


    override fun data3_0410(
        data1: Int, data2: Int, data3: Int, data4: Int, data5: Int, data6: Int
    ) {
        binding.v40.text = "线控在线:$data1"
        binding.v41.text = "左转在线:$data2"
        binding.v42.text = "右转在线:$data3"
        binding.v43.text = "左驱在线:$data4"
        binding.v44.text = "右驱在线:$data5"
        binding.v45.text = "吊装在线:$data6"
        binding.v46.text = "光电开关:$data6"

    }

    override fun data4_0410(data1: Int, data2: Int, data3: Int) {
        binding.v34.text = "是否直线:$data1"
        binding.v35.text = "故障:$data2"
//        binding.v36.text = "行走/堆码:$data3"

    }

    override fun data5_0410(data1: Int, data2: Int) {
        binding.v61.text = "吊重：$data1"
        binding.v62.text = "吊重状态:$data2"

    }

    override fun data6_0410(data1: Int, data2: Int, data3: Int) {
        binding.v37.text = "前保护:$data1"
        binding.v39.text = "后保护:$data2"
        binding.v46.text = "光电开关:$data3"
    }


    override fun isConnect() {

    }

    override fun isUnConnect() {

    }

    override fun serialFaultState(data: Int) {

    }


}